package com.vidmt.api.modules.sys.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.thinkgem.jeesite.common.persistence.CrudDao;
import com.thinkgem.jeesite.common.persistence.annotation.MyBatisDao;
import com.vidmt.api.modules.sys.Enums.LvlType;
import com.vidmt.api.modules.sys.entity.User;

/**
 * 日志DAO接口
 * 
 * @author ThinkGem
 * @version 2014-05-16
 */
@MyBatisDao
public interface UserDao extends CrudDao<User> {

	@Override
	// @Options(useGeneratedKeys = true, keyProperty = "id")
	@Insert("INSERT INTO v_user(id,`name`,nick,email,phone,sex,birth,status,create_date) "
			+ "VALUES(#{id},#{name},#{nick},#{email},#{phone},#{sex},#{birth},#{status},now())")
	public int insert(User entity);

	@Select("SELECT IFNULL(MAX(id),100000) FROM v_user")
	public long maxUid();

	public User load(Long id);
	
	public User findByColumn(@Param("column") String column, String value);

	@Override
	public List<User> findAll();

	@Update("UPDATE v_user SET `${column}`=#{2} WHERE id=#{0}")
	public int updateColumn(Long uid, @Param("column") String column, Object value);
	
	@Override
	@Update("UPDATE v_user SET name=#{name},nick=#{nick},email=#{email}, phone=#{phone},status=#{status},sex=#{sex},birth=#{birth},signature=#{signature},address=#{address},avatar_uri=#{avatarUri}, photo_uri=#{photoUri},flag=#{flag} WHERE id=#{id}")
	public int update(User entity);
	
	@Select("select pwd from v_user where id=#{0}")
	public String getPassword(Long uid);

	public List<User> findByUids(@Param("uids") List<Long> uids);

	@Update("UPDATE v_user SET `status`='PAYED', lvl=#{1},lvl_end=DATE_ADD(IFNULL(lvl_end,NOW()),INTERVAL #{2} SECOND) WHERE id=#{0}")
	public int updateUserPayed(Long uid, LvlType lvltype, Long second);

	@Update("UPDATE v_user SET `status`='NORMAL', lvl=NULL,lvl_end=NULL WHERE id=#{0}")
	public int updateUserExpired(Long uid);

	@Select("SELECT count(0) FROM v_user")
	public long getUserCount();

	@Delete("DELETE FROM v_user WHERE `${column}`=#{1}")
	public int deleteByColumn(@Param("column") String column, Object value);

}
